package org.jeecg.modules.cascv.image.job;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.cascv.image.util.LaptopConstant;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;

@Slf4j
@Service
public class GetFutureToListJob {
    @Scheduled(fixedRate = 100)
    public void exec() {
        if (LaptopConstant.imageFutureList.isEmpty()) {
            return;
        }

        for (CompletableFuture<JSONObject> jsonObjectCompletableFuture : LaptopConstant.imageFutureList) {
            try {
                JSONObject jsonObject = jsonObjectCompletableFuture.get();
                String key = jsonObject.getString("key");
                jsonObject.remove("key");

                    //将图片添加到map
                if(null!=key) {
                    List<JSONObject> list = LaptopConstant.infoMap.get(key);
                    if (list == null) {
                        list = new CopyOnWriteArrayList<>();
                        LaptopConstant.infoMap.put(key, list);
                    }
                    list.add(jsonObject);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
